CTCI 1.1

Determine if a string has all unique characters


In [1]:
def HasUniqueChar(str):
    char_dict = {}
    for letter in str:
        if letter in char_dict:
            return False
        else:
            char_dict[letter] = True
    return True

In [2]:
sent1uniqueness = HasUniqueChar("asdfghjkl")
sent2uniqueness = HasUniqueChar("asdasdfgh")

In [3]:
print sent1uniqueness, sent2uniqueness


True False

(If we are not allwed to use a separate data structure, then we have to use nested for loop, comparing each charcter with all other characters in the ar